#include "mycoin.h"
#include <QPixmap>
#include <QMessageBox>
#include <QDebug>

Mycoin::Mycoin(QString coinStr)
{
    m_isWin = false;
    QPixmap pix;
    int ret = pix.load(coinStr);
    if(!ret)
    {
        QString str = QString("加载图片错误， %1").arg(coinStr);
        QMessageBox::critical(this, "加载失败", str);
        return;
    }

    //设置图片大小
    this->setFixedSize(pix.width(), pix.height());

    //设置不规则图片样式
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图片
    this->setIcon(pix);

    //设置图片大小
    this->setIconSize(QSize(pix.width(), pix.height()));

    m_timer1 = new QTimer(this);
    m_timer2 = new QTimer(this);

    m_min_index = 1;
    m_max_index = 8;


    connect(m_timer1, &QTimer::timeout, [=]()
    {
        QString pixPath = QString(":/Coin000%1.png").arg(m_min_index);

        QPixmap pix;
        int ret = pix.load(pixPath);
        if(!ret)
        {
            QString str = QString("加载图片错误， %1").arg(pixPath);
            QMessageBox::critical(this, "加载失败", str);
            return;
        }

        //设置图片大小
        this->setFixedSize(pix.width(), pix.height());

        //设置不规则图片样式
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图片
        this->setIcon(pix);

        //设置图片大小
        this->setIconSize(QSize(pix.width(), pix.height()));

        m_isAnimation = false;

        m_min_index++;

        if(m_min_index > m_max_index)
        {
            m_isAnimation = false;
            m_timer1->stop();
            m_min_index = 1;
        }

    });

    connect(m_timer2, &QTimer::timeout, [=]()
    {
        QString pixPath = QString(":/Coin000%1.png").arg(m_max_index);

        QPixmap pix;
        int ret = pix.load(pixPath);
        if(!ret)
        {
            QString str = QString("加载图片错误， %1").arg(pixPath);
            QMessageBox::critical(this, "加载失败", str);
            return;
        }

        //设置图片大小
        this->setFixedSize(pix.width(), pix.height());

        //设置不规则图片样式
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图片
        this->setIcon(pix);

        //设置图片大小
        this->setIconSize(QSize(pix.width(), pix.height()));

        m_max_index--;

        if(m_min_index > m_max_index)
        {
            m_isAnimation = false;
            m_timer2->stop();
            m_max_index = 8;
        }
    });

}


void Mycoin::ChangeStatus()
{
    if(m_flag == 1)
    {
        m_flag = 0;
        m_isAnimation = true;
        m_timer1->start(30);
    }
    else
    {
        m_flag = 1;
        m_isAnimation = true;
        m_timer2->start(30);
    }
}


void Mycoin::mousePressEvent(QMouseEvent *e)
{
    if(this->m_isAnimation == true || this->m_isWin == true)
    {
        //正在做动画，不做鼠标的响应
        return;
    }
    else
    {
        //交给父类 做默认处理
        QPushButton::mousePressEvent(e);
    }
}
